package util;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.ces.v1.region.CesRegion;
import com.huaweicloud.sdk.ces.v1.*;
import com.huaweicloud.sdk.ces.v1.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.PropertyResourceBundle;

/**
 * 根据资源ID和资源regionId查询资源使用率
 * 批量查询监控数据
 */
public class BatchListMetricData {

    public static String ak="";
    public static String sk="";
    public static String endPoint="";
    public static void main(String[] args) {
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 start
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("huaweicloud.properties");
            PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
            ak=bundle.getString("ak");
            sk=bundle.getString("sk");
        } catch (IOException e) {
            System.out.println("从resources内properties配置文件中,获取密钥失败。");
            throw new RuntimeException(e);
        }
        /*// Endpoint请按实际情况填写。
        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
        String ak = "UEYW*******KSV";
        String sk = "PnXLi************9su5IkhDRB";*/
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 end

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        CesClient client = CesClient.newBuilder()
                .withCredential(auth)
                .withRegion(CesRegion.valueOf("cn-east-3"))
                .build();
        BatchListMetricDataRequest request = new BatchListMetricDataRequest();
        BatchListMetricDataRequestBody body = new BatchListMetricDataRequestBody();
        List<MetricsDimension> listMetricsDimensions = new ArrayList();
        listMetricsDimensions.add(
                new MetricsDimension()
                        .withName("instance_id")
                        .withValue("1ec325fb-e899-42f9-b472-a215cb9057fa")
        );
        List<MetricInfo> listbodyMetrics = new ArrayList();
        listbodyMetrics.add(
                new MetricInfo()
                        .withNamespace("SYS.ECS")
                        .withMetricName("cpu_util")
                        .withDimensions(listMetricsDimensions)
        );
        body.withTo(1706162230000L);
        body.withFrom(1706158630000L);
        body.withFilter("average");
        body.withPeriod("300");
        body.withMetrics(listbodyMetrics);
        request.withBody(body);
        try {
            BatchListMetricDataResponse response = client.batchListMetricData(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
